package com.orangesoft.common.config;

import com.orangesoft.tescherfriend.business.user.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description 自定义Realm，继承AuthorizingRealm类，重写认证和授权方法
 * @ClassName UserRealm
 * @Author yanchengzhi
 * @date 2021.09.25 15:36
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;

    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        return info;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authToken;
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(token.getUsername(), new String(token.getPassword()), getName());
        return info;
    }
}
